﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Organization;
using TomNet.IMSC.Web.Areas.Organization.Models;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.Organization.Controllers
{
    public class OutSideController : BaseController
    {
        public IOutSideModelContract OutSideModelContract { get; set; }
        public IDepartmentContract DepartmentContract { get; set; }
        public IStaffContract StaffContract { get; set; }
        #region 列表
        [Description("外勤-列表")]
        // GET: Organization/OutSide
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult IndexAsync(int pageIndex, int pageSize, string staff, DateTime month)
        {
            //生成临时类
            var data = from osm in OutSideModelContract.Entities
                       where !osm.IsDeleted && osm.CreatedTime.Year == (month.Year) && osm.CreatedTime.Month == (month.Month)
                       select new OutSideTempModel
                       {
                           Id = osm.Id,
                           StaffId = osm.CreatorId,
                           CreatedTime = osm.CreatedTime + "",
                           ImagePage = osm.ImagePage,
                           Time = osm.CreatedTime,
                           IsLocked = osm.IsLocked
                       };

            if (staff != "")
            {
                data = data.Where(m => m.StaffId == staff);
            }
            //临时类集合按照日起分组
            IEnumerable<IGrouping<string, OutSideTempModel>> outerSequence = data.ToList().GroupBy(o => Convert.ToDateTime(o.CreatedTime).ToString("yyyy-MM-dd"));
            //生成MODEL泛型集合
            List<OutSideOrderbyModel> list = new List<OutSideOrderbyModel>();

            //遍历集合
            foreach (IGrouping<string, OutSideTempModel> item in outerSequence)
            {
                var temp = new OutSideOrderbyModel();  //生成临时MODEL  
                //图片集合
                //List<string> imgs = new List<string>(); 
                //foreach (OutSideTempModel item2 in item)
                //{  
                //    imgs.Add(item2.ImagePage);  
                //}
                temp.Id = item.Select(m => m.Id).ToList();
                temp.CreatedTime = item.Key;
                temp.ImagePage = item.Select(m => m.ImagePage).ToList();
                temp.Number = item.Count();
                temp.Time = item.Select(m => m.Time).ToList();
                temp.IsLocked = item.Select(m => m.IsLocked).ToList();
                list.Add(temp);
            }

            //数据总数
            var total = list.Count();

            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.CreatedTime).ToList();

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()
                : list;

            //此处可以采用匿名对象 GridData<object>
            GridData<object> gridData = new GridData<object>() { Total = total, Rows = list };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        } 
        #endregion

        #region 临时类
        public class OutSideTempModel
        {
            /// <summary>
            /// 图片ID
            /// </summary>
            public int Id { get; set; }
            /// <summary>
            /// 打卡时间
            /// </summary>
            public string CreatedTime { get; set; }
            /// <summary>
            /// 定位图片
            /// </summary>
            public string ImagePage { get; set; }
            /// <summary>
            /// 人员ID
            /// </summary>
            public string StaffId { get; set; }
            /// <summary>
            /// 打卡时间
            /// </summary>
            public DateTime Time { get; set; }
            /// <summary>
            /// 是否合格
            /// </summary>
            public bool IsLocked { get; set; }
        } 
        #endregion



        [Description("外勤-考勤图片")]
        public ActionResult SratImage(string src,string isLocked,string id)
        {
            ViewBag.id = id;
            ViewBag.isLocked = isLocked;
            ViewBag.src = src;
            return View();
        }
        //[HttpPost]
        //public ActionResult InsertClassAsync(OutSideModel entity)
        //{
        //    //所有AJAX的结果，返回统一数据格式
        //    var result = new AjaxResult();
        //    try
        //    {
        //        var query = QandATypeContract.CheckExists(m => m.ProblemType.Equals(entity.ProblemType) && !m.IsDeleted);
        //        if (query)
        //        {
        //            result.Type = AjaxResultType.Error;
        //            result.Content = "已经存在相同的分类";
        //        }
        //        else
        //        {
        //            var count = QandATypeContract.Insert(entity);
        //            if (count > 0)
        //            {
        //                result.Type = AjaxResultType.Success;
        //                result.Content = "录入成功";
        //            }
        //            else
        //            {
        //                result.Type = AjaxResultType.Error;
        //                result.Content = "录入失败";
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        result.Type = AjaxResultType.Error;
        //        result.Content = "异常操作";
        //    }
        //    return JsonEx(result);
        //}


        #region 下拉框
        /// <summary>  
        /// 部门
        /// </summary>  
        [AjaxOnly]
        public ActionResult GetDepartmentlist()
        {
            //获取省份数据   
            var queryResult = DepartmentContract.Entities;
            return JsonEx(queryResult.ToList(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>  
        /// 获取城市  
        /// </summary>  
        /// <param name="did"></param>  
        /// <returns></returns>  
        [AjaxOnly]
        public ActionResult GetStafflist(int did)//根据省份ID获取城市  
        {
            var list = StaffContract.Entities.Where(m => m.DepartmentID.Equals(did)).ToList();//城市ID对应值  
            return JsonEx(list, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}